﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Model.Upadan;
using Model.Sadharon;
using Model.Poribar.SizePanjeri;

namespace Model.Poribar.SizePanjeri
{
    public class SizeKazImpl : SizeKaz
    {
        private ProductSize size;
        private Result result;
        private DBSadharon idGenerator;
        public SizeKazImpl() {

            result = new Result();
            idGenerator = new DBSadharon();
            size = new ProductSize();
        }

        public Result create(Object sizeObj)
        {
            size = (ProductSize)sizeObj;
            size.Id = idGenerator.getUniqId(DatabaseConnection.GetConnection(),"ProductSize","PS");
            string insertString = "insert into  ProductSize values(" + "'" + size.Id + "','" + size.Name + "','" + size.isLive + "')"; ;
            try
            {
                MakeQuery(insertString).ExecuteNonQuery();
                result.IsSuccess = true;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            } 
            return result;
        }

        public Result getById(string id)
        {
            string getString = "Select * from ProductSize Where Id='" + id + "' and Live='L'";
            SqlDataReader productSizeReader;

            try
            {
                productSizeReader = MakeQuery(getString).ExecuteReader();
                if (productSizeReader.HasRows)
                {
                    while (productSizeReader.Read())
                    {
                        size.Id = productSizeReader["Id"].ToString();
                        size.Name = productSizeReader["Name"].ToString();
                    }
                    result.ResultObj = size;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally {
                DatabaseConnection.sqlConnect.Close();
            }
          
            return result;
        }

        public Result getByName(string name)
        {
            string getString = "Select * from ProductSize Where Name='" + name + "' and Live='L'";
            SqlDataReader productSizeReader;

            try
            {
                productSizeReader = MakeQuery(getString).ExecuteReader();
                if (productSizeReader.HasRows)
                {
                    while (productSizeReader.Read())
                    {
                        size.Id = productSizeReader["Id"].ToString();
                        size.Name = productSizeReader["Name"].ToString();
                    }
                    result.ResultObj = size;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }

            return result;
        }        

        public Result All()
        {
            string findString = "select * from ProductSize where Live='L'";
            try
            {
                SqlDataReader productSizeReader = MakeQuery(findString).ExecuteReader();
                if (productSizeReader.HasRows)
                {
                    List<ProductSize> allSize = new List<ProductSize>();
                    while (productSizeReader.Read())
                    {
                        size = new ProductSize();
                        size.Name = productSizeReader["Name"].ToString();
                        size.Id = productSizeReader["Id"].ToString();
                        size.isLive = (char)productSizeReader["Live"].ToString().ToCharArray()[0];
                        allSize.Add(size);
                    }
                    result.ResultObj = allSize;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }


        public Result update(Object _product)
        {
            size = (ProductSize) _product;
            string updateString = "update ProductSize set  Name='"+ size.Name +"' where Id='" + size.Id + "'";
            try
            {
                SqlDataReader productSizeReader = MakeQuery(updateString).ExecuteReader();
                if (productSizeReader.HasRows)
                {
                    while (productSizeReader.Read())
                    {
                        size.Id = productSizeReader["Id"].ToString();
                        size.Name = productSizeReader["Name"].ToString();
                    }
                    result.ResultObj = size;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            } 
            return result;
        }
        
        public Result delete(string _id)
        {
            string updateString = "update ProductSize set  Live='D' where Id='" + _id + "'";
            try
            {
                SqlDataReader productSizeReader = MakeQuery(updateString).ExecuteReader();
                if (productSizeReader.HasRows)
                {
                    while (productSizeReader.Read())
                    {
                        size.Id = productSizeReader["Id"].ToString();
                        size.Name = productSizeReader["Name"].ToString();
                        size.isLive = 'D';
                    }
                    result.ResultObj = size;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            } 
            return result;
        }

        private SqlCommand MakeQuery(string queryString)
        {
            DatabaseConnection.GetConnection().Open();
            return (new SqlCommand(queryString, DatabaseConnection.sqlConnect));
        }

    }
}
